﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace Serie_Counter.DataLayer
{
    public class IO_Covers
    {
        /// <summary>
        /// Save the cover in the stream to a JPG file
        /// </summary>
        /// <param name="path">save path ex: Covers/test.jpg</param>
        /// <param name="coverStream">Stream containing the image</param>
        /// <returns></returns>
        public static Boolean saveCover(string path, MemoryStream coverStream)
        {
            try
            {
                Image cover = Image.FromStream(coverStream);
                using (FileStream writer = new FileStream(path,FileMode.Create,FileAccess.Write))
                {
                    cover.Save(writer,ImageFormat.Jpeg );
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                coverStream.Close();
            }
        }

        /// <summary>
        /// Remove a cover from the fileSystem
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool removeCover(string path)
        {
            try
            {
                File.Delete(path);
                return true;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return false;
            }
        }

        /// <summary>
        /// gets a list of old cover paths
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<string> getOldCoversPathsFile(string path)
        {
            try
            {
                using (FileStream read = new FileStream(path,FileMode.Open,FileAccess.Read))
                {
                    BinaryFormatter binaryFormatter = new BinaryFormatter();
                    return (List<string>) binaryFormatter.Deserialize(read);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return new List<string>();
            }
        }

        /// <summary>
        /// save a list of old cover paths
        /// </summary>
        /// <param name="path"></param>
        /// <param name="oldCoverPaths"></param>
        public static void saveOldCoversPathsFile(string path, List<string> oldCoverPaths)
        {
            using (FileStream writer = new FileStream(path,FileMode.Create,FileAccess.Write))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(writer, oldCoverPaths);
            }
        }
    }
}
